from flask import Flask
from flask_cors import CORS  # 解决跨域问题
from app.config import Config
from app.routes import api_bp
from app.extensions import init_jwt  # 导入JWT初始化函数
from app.routes.auth import auth_bp  # 导入auth蓝图
from app.admin.routes.auth import admin_auth_bp
from app.admin.routes.user_manage import user_manage_bp
from flask_jwt_extended import JWTManager
from app.routes.ar_culture import ar_culture_bp  
from app.routes.user import user_bp  # 新增导入user_bp

def create_app():
    """应用工厂函数：创建并配置Flask应用"""
    app = Flask(__name__)
    app.register_blueprint(auth_bp)  # 注册蓝图，关键步骤
    app.config.from_object(Config)  # 加载配置
    
    app.register_blueprint(admin_auth_bp)
    app.register_blueprint(user_manage_bp)
    app.register_blueprint(ar_culture_bp)
    app.register_blueprint(user_bp)  # 新增注册user_bp
    
    # 初始化JWT
    init_jwt(app)
    
    # 注册蓝图
    from app.routes.image_recognition import image_recognition_bp
    app.register_blueprint(image_recognition_bp)

    # 允许跨域（前端小程序域名需在微信公众平台配置）
    CORS(app, resources={r"/api/*": {"origins": "*"}})
    
    # 注册路由蓝图（所有接口路径前缀为/api）
    app.register_blueprint(api_bp)
    
    return app